package com.tgky.ybzb.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.tgky.ybzb.entity.RwghmxZbs;
import com.tgky.ybzb.utils.excel.ExcelExport;
import com.tgky.ybzb.utils.excel.ExcelUtils;
import com.tgky.ybzb.vo.GcxqVo;
import org.apache.coyote.Response;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.tgky.ybzb.entity.Gcxq;
import com.tgky.ybzb.service.IGcxqService;
import com.tgky.ybzb.common.IdParam;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.common.Result;
import com.tgky.ybzb.utils.MPUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 观察需求表 前端控制器
 *
 * @author XYP
 * @since 2024-07-22
 */
@RestController
@AllArgsConstructor
@Slf4j
@Api(value = "观察需求表", tags = "观察需求表接口")
@RequestMapping("gcxq")
public class GcxqController {

    private IGcxqService gcxqService;

    /**
     * 评估-业务运行调度
     *
     * @return
     */
    @PostMapping("/assessYwyxdd ")
    @ApiOperation(value = "评估-业务运行调度")
    public Result assessYwyxdd() {

        return Result.success();
    }

    /**
     * 调用决策算法模型
     *
     * @return
     */
    @PostMapping("/useJcmx")
    @ApiOperation(value = "调用决策算法模型")
    public Result useJcssmx() {

        return Result.success();
    }
    /**
     * 导出观测需求列表页
     *
     * @return
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出观测需求列表")
    public void exportGcxqList(HttpServletResponse response) {
        QueryWrapper<Gcxq> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<Gcxq> gcxqList=gcxqService.list(queryWrapper);
        List<GcxqVo> list=new ArrayList<>();
        gcxqList.stream().forEach(item->{
            GcxqVo gcxqVo=new GcxqVo();
            BeanUtil.copyProperties(item,gcxqVo);
            list.add(gcxqVo);
        });
        ExcelUtils.export(response,"观测需求决策数据",list,GcxqVo.class);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入gcxq")
    public Result save(@RequestBody Gcxq gcxq) {
        if (gcxq == null) {
            return Result.error("参数异常");
        }
        return Result.status(gcxqService.save(gcxq));
    }

    /**
     * 修改 观察需求表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入gcxq")
    public Result update(@RequestBody Gcxq gcxq) {
        if (gcxq == null) {
            return Result.error("参数异常");
        }

        return Result.status(gcxqService.updateById(gcxq));
    }

    /**
     * 删除 观察需求表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除", notes = "传入ids")
    public Result remove(@RequestBody IdParam<Long> idParam) {
        if (idParam == null) {
            return Result.error("参数异常！");
        }

        return Result.status(gcxqService.removeByIds(idParam.getIds()));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情", notes = "传入id")
    public Result<Gcxq> detail(Long id) {
        if (id == null) {
            return Result.error("参数异常");
        }
        Gcxq detail = gcxqService.getById(id);
        return Result.successData(detail);
    }

    /**
     * 查询 观察需求表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询", notes = "传入gcxq")
    public Result list(PageParam pageParam, Gcxq gcxq) {
        List<Gcxq> list = gcxqService.list(gcxqService.getQueryWrapper(pageParam, gcxq));
        return Result.successData(list);
    }

    /**
     * 分页 观察需求表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页", notes = "传入gcxq")
    public Result<IPage> page(PageParam pageParam, Gcxq gcxq) {
        IPage<Gcxq> pages = gcxqService.page(
                MPUtil.getPage(pageParam),
                gcxqService.getQueryWrapper(null, gcxq)
        );
        return Result.successData(pages);
    }


}
